#include<iostream>
#include<vector>
#include<deque>
using namespace std;

void solve(vector<int> v, int k)
{
  int n = v.size();
  deque<int> q;
  for(int i = 0; i < n; i++)
  {
    while(!q.empty() && v[q.back()] > v[i]) q.pop_back();
    q.push_back(i);
    if(i - q.front() == k) 
    {
      q.pop_front();
    }
    if(i >= k - 1) 
    {
      cout << " ";
      cout << v[q.front()];  
    }
  }
  cout << endl;
  q.clear();
   for(int i = 0; i < n; i++)
  {
    while(!q.empty() && v[q.back()] < v[i]) q.pop_back();
    q.push_back(i);
    if(i - q.front() == k) 
    {
      q.pop_front();
    }
    if(i >= k - 1) 
    {
      cout << " ";
      cout << v[q.front()];  
    } 
  }
}

int main()
{
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for(int i = 0 ;i < n; i++) cin >> v[i];
  solve(v, k);
  return 0;
}
